package contacts530.application;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Update extends JDialog implements ActionListener {

	private JPanel p1, p2;
	private JLabel name,gender,age,dbo,phone,email,qq;
	private JTextField t_name,t_gender,t_age,t_dbo,t_phone,t_email,t_qq;
	private JButton sure, cancel;
    JLabel l_dbo,l_email;
    public Update(Frame s, String c, Boolean m, Select sel, int line) {
       super(s, c, m);      
       name=new JLabel("姓名"); 
       name.setBounds(40,50,60,40);
       gender=new JLabel("宿舍号");
       gender.setBounds(40,100,60,40);
       age=new JLabel("年龄");
       age.setBounds(40,150,60,40);
       dbo=new JLabel("生日");
       dbo.setBounds(40,200,60,40);
       phone=new JLabel("手机");
       phone.setBounds(40,250,60,40);
       email=new JLabel("Email");
       email.setBounds(40,300,60,40);
       qq=new JLabel("QQ"); 
       qq.setBounds(40,350,60,40);
       
        t_name = new JTextField();
        t_name.setBounds(100, 55, 100, 25);
        String st = (String) sel.getValueAt(line, 0);
        t_name.setText(st);

        t_gender = new JTextField();
        t_gender.setBounds(100, 105, 100, 25);
        String st1 = (String) sel.getValueAt(line, 1);
        t_gender.setText(st1);

        t_age = new JTextField();
        t_age.setBounds(100, 155, 100, 25);
        String st2 = (String) sel.getValueAt(line, 2).toString();
        t_age.setText(st2);

        t_dbo = new JTextField();
        t_dbo.setBounds(100, 205, 100, 25);
        String st3 = (String) sel.getValueAt(line, 3).toString();
        t_dbo.setText(st3);    
        l_dbo = new JLabel("如：1995");
        l_dbo.setBounds(100,228,125,25);
      
        t_phone = new JTextField();
        t_phone.setBounds(100, 255, 100, 25);
        String st4 = (String) sel.getValueAt(line, 4).toString();
        t_phone.setText(st4);
        
        t_email = new JTextField();
        t_email.setBounds(100, 305, 100, 25);
        String st5 = (String) sel.getValueAt(line, 5).toString();
        t_email.setText(st5);
        l_email=new JLabel("如:1356@qq.com");
        l_email.setBounds(100,328,150,25);
        
        t_qq = new JTextField();
        t_qq.setBounds(100,355, 100, 25);
        String st6 = (String) sel.getValueAt(line, 6).toString();
        t_qq.setText(st6);

        sure=new JButton("确定");
        sure.addActionListener(this);
        sure.setBounds(55,450,60,30);
        cancel=new JButton("取消");
        cancel.setBounds(135,450, 60,30);
        cancel.addActionListener(this);

      this.add(name);
      this.add(gender);
      this.add(age);
      this.add(dbo);
      this.add(phone);
      this.add(email);
      this.add(qq);   
      this.add(t_name);
      this.add(t_gender);
      this.add(t_age);
      this.add(t_dbo);
      this.add(t_phone);
      this.add(t_email);
      this.add(t_qq);
      this.add(sure);
      this.add(cancel);
      this.setTitle(c);
      this.setSize(270,600);
      this.setLayout(null);
      this.setVisible(true);
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String button = e.getActionCommand();
        if (button.equals("取消")) {
            this.dispose();
        } else if (button.equals("确定")) {
            System.out.println(name+"更新成功");
                         PreparedStatement ps = null;
                         Connection ct = null;
                         ResultSet rs = null;
            try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://localhost;database =contactsdb;user=sa;password=zyn416427";
        ct = DriverManager.getConnection(url);
        ps = ct.prepareStatement("update student set name=?,gender=?,age=?,dbo=?,phone=?,email=?,qq=? where name='"+name+"'");
           ps.setString(1,t_name.getText());
           ps.setString(2,t_gender.getText());
           ps.setString(3,t_age.getText());
           ps.setString(4,t_dbo.getText());
           ps.setString(5,t_phone.getText());
           ps.setString(6,t_email.getText());
           ps.setString(7,t_qq.getText());
           System.out.println(t_name.getText()+"更新成功");
                ps.executeUpdate();
                this.dispose();
            } catch (ClassNotFoundException ce) {
                System.out.println("ERROR:"+ce);
            }catch(SQLException se){
                JOptionPane.showMessageDialog(this,"信息输入有误");
            }      
            finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (ps != null) {
                        ps.close();
                    }
                    if (ct != null) {
                        ct.close();
                    }
                } catch (SQLException e2) {
                    System.out.println("ERROR:"+e2);
                }
            }
        }
    }
}
